package Domain;

import Domain.Entity;

/**
 * Holds a domain object that is a real-life object stored in the database. 
 * Common for all entities in the database are that they have an
 * internal identity ('id') which is its primary key in the database. 
 * 
 * @author Daniel Fagerberg
 * 
 */
public abstract class Entity {

	private int id;

	/**
	 * Constructor that sets the internal identifier ('id') to -1. 
	 */
	public Entity() {
		this.id = -1;
	}
	
	/**
	 * Constructor that sets the internal identifier ('id') of this
	 * {@link Entity} object.
	 * 
	 * @param id
	 */
	public Entity(int id) {
		this.id = id;
	}

	/**
	 * Returns the internal identifier of the {@link Entity} object.
	 * 
	 * @return the internal identifier of the {@link Entity} object.
	 */
	public int getId() {
		return id;
	}

	/**
	 * Sets the internal identifier of the {@link Entity} object.
	 * 
	 * @param id
	 *            the internal identifier of the {@link Entity} object.
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Equals between {@link Entity} objects means that the objects are of the
	 * same subclass and share the same internal identifier ('id'). The equals
	 * check ignore the values of other fields.
	 * 
	 * @param obj
	 *            the object that is tested for equality
	 * @return true if the two objects share the same internal identity key
	 *         ('id').
	 */
	@Override
	public boolean equals(Object obj) {
		return obj instanceof Entity && this.getClass().equals(obj.getClass())
				&& this.id == ((Entity) obj).id;
	}

}
